﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace SyCODE.Blog.Task
{
    /// <summary>
    /// 任务处理器
    /// </summary>
    public class Processor
    {
        /// <summary>
        /// 任务执行进程的实现
        /// </summary>
        private sealed class Worker
        {
            private Thread worker;
            private Queue<TaskBase> queue;
            private bool working = false;

            /// <summary>
            /// 任务执行进程的构造函数
            /// </summary>
            public Worker()
            {
            }


            /// <summary>
            /// 启动任务进程
            /// </summary>
            public void Start()
            {
                if (!this.working)
                {
                    this.queue = Serialization.Deserialize();

                    this.worker = new Thread(new ThreadStart(WorkerHandler));
                    this.worker.IsBackground = true;
                    this.worker.Priority = ThreadPriority.BelowNormal;
                    this.worker.Start();
                }
            }

            /// <summary>
            /// 停止任务进程
            /// </summary>
            public void Stop()
            {
                if (this.working)
                {
                    this.working = false;
                    this.worker.Join();
                    Serialization.Serialize(queue);
                }
            }

            /// <summary>
            /// 指派一个新的任务
            /// </summary>
            /// <param name="task"></param>
            /// <returns></returns>
            public int Assign(TaskBase task)
            {
                this.Start();
                this.queue.Enqueue(task);
                return this.queue.Count;
            }

            ~Worker()
            {
                this.Stop();
            }

            /// <summary>
            /// 任务循环处理函数
            /// </summary>
            private void WorkerHandler()
            {
                this.working = true;
                while (this.working)
                {
                    while (this.queue.Count > 0)
                    {
                        TaskBase task = queue.Dequeue();
                        try
                        {
                            task.Execute();
                        }
                        finally
                        {
                        }
                    }
                    Thread.Sleep(100);
                }
            }
        }

        private static Worker worker = new Worker();
        /// <summary>
        /// 向任务处理器指派新的任务
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        public static int Assign(TaskBase task)
        {
            return worker.Assign(task);
        }

       /// <summary>
        /// 启动任务处理器
       /// </summary>
        public static void Start()
        {
            worker.Start();
        }

        /// <summary>
        /// 停止任务处理器
        /// </summary>
        public static void Stop()
        {
            worker.Stop();
        }
    }
}